package com.doform.android.xianggucheng.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.adapter.CommonListAdapter;
import com.doform.android.xianggucheng.bean.CategoryBean;
import com.doform.android.xianggucheng.bean.GuidBean;
import com.doform.android.xianggucheng.holder.ViewHolder;
import com.doform.android.xianggucheng.popwindow.IPopWindow;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.TsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/12.
 */

public class NewCategoryActivity extends BaseTitleActivity {

    @BindView(R.id.tv_category)
    TextView tvCategory;

    @BindView(R.id.ll)
    LinearLayout mLl;

    @BindView(R.id.et_name)
    EditText etName;

    @BindView(R.id.et_sort)
    EditText etSort;

    private String fId;
    private String mCategory = "-请选择-";

    private IPopWindow mPopupWindow;
    private List<CategoryBean.ListBean> mCategoryDatas = new ArrayList<>();
    private CommonListAdapter<CategoryBean.ListBean> mCommonAdapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_new_and_update_category;
    }

    private List<CategoryBean.ListBean> mDatas = new ArrayList<>();

    @Override
    protected void init() {
        if (mPopupWindow == null) {
            mPopupWindow = new IPopWindow(this, R.layout.pop_new_add_goodsk);
        }
        etSort.setText("255");
    }

    @Override
    protected void initData() {
        getDataForService();
    }

    private void getDataForService() {

        HttpManager.instance()
                .getCategory2("1")
                .subscribe(new ErrorHandleSubscriber<CategoryBean>() {
                    @Override
                    public void onNext(@NonNull CategoryBean categoryBean) {
                        notifyData(categoryBean.getList());
                    }
                });
    }

    private void notifyData(List<CategoryBean.ListBean> data) {
        mCategoryDatas.clear();
        mCategoryDatas.addAll(data);
        if (mCommonAdapter != null) {
            mCommonAdapter.notifyDataSetChanged();
        }
    }


    @Override
    protected void initListener() {

        mCommonAdapter = new CommonListAdapter<CategoryBean.ListBean>(this, mCategoryDatas, R.layout.pop_item) {

            @Override
            protected void convert(ViewHolder vh, final List<CategoryBean.ListBean> datas, final int position) {

                final CategoryBean.ListBean bean = datas.get(position);

                TextView tv = vh.getView(R.id.tv);
                tv.setText(bean.getClsname());

                tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        fId = bean.getId();
                        mCategory = bean.getClsname();
                        tvCategory.setText(mCategory);
                        mPopupWindow.dismiss();
                    }
                });
            }
        };

        mPopupWindow.setPopAdapter(mCommonAdapter);
        mPopupWindow.setHeadName("-请选择-");
        mPopupWindow.setHeadListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
            }
        });
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("新增分类");
        tvCategory.setText(mCategory);
    }

    @OnClick({R.id.tv_category, R.id.btn_submit})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.tv_category:
                mPopupWindow.showAtLocation(mLl, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.btn_submit:
                submit();
                break;
            default:
                break;
        }
    }

    private void submit() {

        String mName = etName.getText().toString().trim();
        String mSort = etSort.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            TsUtils.showToast("请输入分类名称");
            return;
        } else if (TextUtils.isEmpty(mSort)) {
            mSort = "255";
        } else if (TextUtils.isEmpty(fId)) {
            fId = "0";
        }
        postData(mName, mSort, fId);
    }

    private void postData(String name, String sort, final String fid) {

        HttpManager.instance()
                .addCategory2(name, sort, fid)
                .subscribe(new ErrorHandleSubscriber<GuidBean>() {
                    @Override
                    public void onNext(@NonNull GuidBean str) {
                        finish();
                    }
                });
    }
}
